package com.lzxzjy.web.servlet;

import com.alibaba.fastjson.JSON;
import com.lzxzjy.pojo.Message;
import com.lzxzjy.pojo.PageBean;
import com.lzxzjy.serivce.MessageService;
import com.lzxzjy.serivce.impl.MessageServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/message/*")
public class MessageServlet extends BaseServlet {
    private final MessageService messageService = new MessageServiceImpl();

    public void insertMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader messageReader = request.getReader();
        String messageLine = messageReader.readLine();
        Message message = JSON.parseObject(messageLine, Message.class);
        message.setMessageState(0);
        boolean isSuccess = messageService.insertMessage(message);
        if (isSuccess) {
            response.getWriter().write("success");
        } else {
            response.getWriter().write("failure");
        }
    }

    public void deleteMessageByMessageId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int messageId = Integer.parseInt(request.getReader().readLine());
        boolean isSuccess = messageService.deleteMessageById(messageId);
        if (isSuccess) {
            response.getWriter().write("success");
        } else {
            response.getWriter().write("failure");
        }
    }

    public void readMessageByReaderId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader messageReader = request.getReader();
        String messageLine = messageReader.readLine();
        Message message = JSON.parseObject(messageLine, Message.class);
        message.setMessageTitle("%%");
        message.setMessageContent("%%");
        message.setMessageReturn("%%");

        PageBean<Message> messagePageBean = messageService.selectMessageByPageAndTerms(message);
        boolean isSuccess = false;

        for (Message mess : messagePageBean.getRows()) {
            mess.setMessageReturnState(1);
            isSuccess = messageService.updateMessageById(mess);
        }

        if (isSuccess) {
            response.getWriter().write("success");
        } else {
            response.getWriter().write("failure");
        }
    }

    public void readMessageByMessageId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader messageReader = request.getReader();
        String messageLine = messageReader.readLine();
        Message message = JSON.parseObject(messageLine, Message.class);

        message.setMessageReturnState(1);
        boolean isSuccess = messageService.updateMessageById(message);
        if (isSuccess) {
            response.getWriter().write("success");
        } else {
            response.getWriter().write("failure");
        }
    }

    public void updateMessageByMessageId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader messageReader = request.getReader();
        String messageLine = messageReader.readLine();
        Message message = JSON.parseObject(messageLine, Message.class);

        message.setMessageState(1);
        boolean isSuccess = messageService.updateMessageById(message);
        if (isSuccess) {
            response.getWriter().write("success");
        } else {
            response.getWriter().write("failure");
        }
    }

    public void selectMessageByPageAndTerms(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader messageReader = request.getReader();
        String messageLine = messageReader.readLine();
        int currentPage = Integer.parseInt(request.getParameter("currentPage"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));

        Message message = JSON.parseObject(messageLine, Message.class);

        message.setMessageTitle("%" + message.getMessageTitle() + "%");
        message.setMessageContent("%" + message.getMessageContent() + "%");
        message.setReaderName("%" + message.getReaderName() + "%");
        message.setMessageReturn("%" + message.getMessageReturn() + "%");
        PageBean<Message> messagePageBean = messageService.selectMessageByPageAndTerms(message, currentPage, pageSize);
        String messagePageBeanLine = JSON.toJSONString(messagePageBean);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(messagePageBeanLine);
    }
}
